import tornado.web
import tornado.httpserver
from tornado.options import define, options
from tornado.web import StaticFileHandler
import tornado.gen
WebPort = 3389 
define("port", default=WebPort, type=int)
print("[INFO]:成功启动聊天服务，端口:",WebPort)
User = {"user1":"123456","user2":"654321"}
global Online
Online = {}

class ExampleHandler(tornado.web.RequestHandler):
	def get(self):
		self.render("pages/test.html")
class ApiHandler(tornado.web.RequestHandler):
	@tornado.gen.coroutine
	def post(self):
		global Online
		context = self.get_argument("context")
		userID  = self.get_argument("userID")
		userPassword = self.get_argument("userPassword")
		if context == "login":
			if userID in User:
				if User[userID] == userPassword:
					self.write("ok")
					Online[userID] = [25]
				else:
					self.write("Password Error")
			else:
				self.write("No User Info")
			self.finish()
		if context == "send-text":
			toWho = self.get_argument("toWho")
			content = self.get_argument("content")
			if (userID in User) and (userID in Online):
				if User[userID] == userPassword:
					if toWho in Online:
						Online[toWho].append([userID,content])
						self.write("ok")
					else:
						self.write("This User Not Online")
				else:
					self.write("Password Error")
			else:
				self.write("No User Info")
			self.finish()
		if context == "longcheck":
			if userID in User:
				if (User[userID] == userPassword) and (userID in Online):
					Online[userID][0] = 20
					if len(Online[userID]) == 1: 
						for i in range(0,100):
							if len(Online[userID]) == 1:
								yield tornado.gen.sleep(0.1)
							else:
								break
						self.write("No Msg")
					else:
						print(Online)
						self.write(Online[userID][1][0] + "$$" + Online[userID][1][1])
						del Online[userID][1]
				else:
					self.write("Error")
			else:
				self.write("Error")
			self.finish()
	@tornado.gen.coroutine
	def get(self):
		context = self.get_argument("context")
		delete = []
		if context == "Local-Check-Online":
			for key in Online:
				Online[key][0] -= 5
				if Online[key][0] <= 0:
					delete.append(key)
		for key in delete:
			del Online[key]
		self.write(str(Online))


class Application(tornado.web.Application):
	def __init__(self):
		handlers = [
			(r"/", ExampleHandler),
			(r"/api", ApiHandler),
			(r"/js/theme/default/(.*)", StaticFileHandler,{"path":"D:\\Pro\\Python\\Web2-Text\\pages\\js\\theme\\default"}),
			(r"/js/theme/(.*)", StaticFileHandler,{"path":"D:\\Pro\\Python\\Web2-Text\\js\\theme"}),
			(r"/img/(.*)", StaticFileHandler,{"path":"D:\\Pro\\Python\\Web2-Text\\pages\\img"}),
			(r"/js/(.*)", StaticFileHandler,{"path":"D:\\Pro\\Python\\Web2-Text\\pages\\js"}),
			(r"/css/(.*)", StaticFileHandler,{"path":"D:\\Pro\Python\\Web2-Text\\pages\\css"}),
			(r"/(.*)", StaticFileHandler,{"path":"D:\\Pro\\Python\\Web2-Text\\pages"})
		]
		settings = dict()
		tornado.web.Application.__init__(self, handlers, settings)

if __name__ == "__main__":
	tornado.options.parse_command_line()
	http_server = tornado.httpserver.HTTPServer(Application())
	http_server.listen(options.port)
	tornado.ioloop.IOLoop.instance().start()
